package com.lkm.market2_0.m_imple.newDataUp;

import com.lkm.frame.P;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.DateHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.StallEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.RandomStringUtils;
import com.lkm.market2_0.m.dataUp.SZCalibrationInfo;
import com.lkm.market2_0.m.dataUp.SZUpdateStatus;
import com.lkm.market2_0.m.dataUp.SZWeightInfo;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;



public abstract class NewDataUpParse {


    

    

    public static SZUpdateStatus parseSZUpdateStatus(SteelyardInfo steelyardInfo, boolean isExceptions) {
        SZUpdateStatus szWeightInfo = new SZUpdateStatus();
        szWeightInfo.macAddr = steelyardInfo.getSteelyardMac();
        szWeightInfo.deviceNo = steelyardInfo.getB_id();
        szWeightInfo.deviceModel = steelyardInfo.deviceModel;
        szWeightInfo.factoryName = "广州市中兴电子衡器厂";

        szWeightInfo.productionDate = steelyardInfo.deviceAge;

        szWeightInfo.userStatus = isExceptions ? 1 : 0;

        StallEntity stallEntity = steelyardInfo.getStallEntity();
        if (stallEntity != null) {
            szWeightInfo.stallCode = "" + stallEntity.getStallNo();
            szWeightInfo.businessEntity = stallEntity.businessentity;
            szWeightInfo.creditCode = stallEntity.creditcode;
        }


        return szWeightInfo;
    }

    public static SZCalibrationInfo parseSZCalibrationInfo(SteelyardInfo steelyardInfo,SteelyardValue steelyardValue ,OrderEntity.GoodEntity goodEntity) {






        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateHelp.DateFormatyyyy_MM_dd_HHmmss, Locale.getDefault());


        SZCalibrationInfo szWeightInfo = new SZCalibrationInfo();

        szWeightInfo.macAddr = steelyardInfo.getSteelyardMac();
        szWeightInfo.deviceNo = steelyardInfo.getB_id();
        szWeightInfo.deviceModel = steelyardInfo.deviceModel;
        szWeightInfo.factoryName = "广州市中兴电子衡器厂";

        szWeightInfo.productionDate = steelyardInfo.deviceAge;

        szWeightInfo.calibrationWeight = ""+steelyardValue.xzfmz/1000f;

        szWeightInfo.offsetZeroAD = ""+goodEntity.offsetZeroAD;

        szWeightInfo.weightAd = ""+goodEntity.ad;

        szWeightInfo.calibrationTime = simpleDateFormat.format(new Date());

        return szWeightInfo;
    }

    

    public static List<SZWeightInfo> parseSZWeightInfo(SteelyardInfo steelyardInfo, OrderEntity orderEntity) {

        List<OrderEntity.GoodEntity> goodEntities = orderEntity.getGoods();
        if (goodEntities == null) return null;

        if (orderEntity.totalPrice <= 0) {

            return null;
        }
        if (CollectionHelp.isEmpty(goodEntities)) return null;

        List<SZWeightInfo> szWeightInfos = new ArrayList<>(goodEntities.size());


        int orderType = 0;






        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateHelp.DateFormatyyyy_MM_dd_HHmmss, Locale.getDefault());


        for (OrderEntity.GoodEntity gn : goodEntities) {
            if (gn == null || gn.weight < 0.01) {
                continue;
            }


//         废弃    int kvalue = (int) ((gn.ad - gn.offsetZeroAD)*10000/gn.weight*1000)/1000;


            int kvalue = (int)( (gn.ad - gn.offsetZeroAD)/gn.weight) ;
            // 由于江工用的是0.1克的精度 所以gn.kValue就是0.1克的。所以需要乘10
            kvalue = kvalue * 10;

            P.p("=== js原k "+gn.kValue);
            P.p("=== js本k "+kvalue);
            float kk = (kvalue/(float)gn.kValue);
            P.p("=== jsk " + kk);


            
            if (kk > 1.05 || kk < 0.95){
                System.out.println("=====sz js k不合格");
                
                continue;
            }



//            double jsWZeroAD = ((gn.ad - gn.offsetZeroAD)*10000/gn.kValue);
//            double jsW = ((gn.ad - gn.zeroAD)*10000/gn.kValue);


//            P.p("=== jsweight "+gn.weight*1000);


            SZWeightInfo szWeightInfo = new SZWeightInfo();


            szWeightInfo.unitType = gn.unitType == 2 ? 1 : 0;
            szWeightInfo.orderType = orderType;


            szWeightInfo.amounts = "" + orderEntity.totalPrice;

            szWeightInfo.macAddr = steelyardInfo.getSteelyardMac();
            szWeightInfo.deviceNo = steelyardInfo.getB_id();
            szWeightInfo.deviceModel = steelyardInfo.deviceModel;
            szWeightInfo.factoryName = "广州市中兴电子衡器厂";

            szWeightInfo.productionDate = steelyardInfo.deviceAge;


            szWeightInfo.orderNo = orderEntity.orderNum;

            if (StringHelp.isEmpty(szWeightInfo.orderNo)) {
                szWeightInfo.orderNo = ServerTimeSync.getInstance().getServerCurTime() + RandomStringUtils.random(4, RandomStringUtils.azAZ90);
            }

            szWeightInfo.goodsCode = gn.goodId;
            szWeightInfo.goodsName = gn.goodName;
            if(StringHelp.isEmpty(szWeightInfo.goodsName)){
                szWeightInfo.goodsName = "商品";
            }

            szWeightInfo.orderTime = simpleDateFormat.format(new Date(gn.getCreateTimestamp()));
            szWeightInfo.initAd = gn.zeroAD;
            szWeightInfo.zeroAd = gn.offsetZeroAD;


            szWeightInfo.weightAd = gn.ad;

            szWeightInfo.weight = "" + gn.weight;
            szWeightInfo.price = "" + gn.unitPrice;
            szWeightInfo.goodimg_on = gn.img_on;

            StallEntity stallEntity = steelyardInfo.getStallEntity();
            if (stallEntity != null) {
                szWeightInfo.stallCode = "" + stallEntity.getStallNo();
                szWeightInfo.businessEntity = stallEntity.businessentity;
                szWeightInfo.creditCode = stallEntity.creditcode;
            }
            szWeightInfos.add(szWeightInfo);

        }
//        System.out.println("====szWeightInfos "+new Gson().toJson(szWeightInfos));
        if (!szWeightInfos.isEmpty()) {

            boolean isok = true;
            for (int i = 0; i < szWeightInfos.size(); i++) {
                SZWeightInfo szWeightInfo = szWeightInfos.get(i);
                if (szWeightInfo.weightAd <= 0 || szWeightInfo.zeroAd <= 0 || szWeightInfo.initAd <= 0) {
                    isok = false;
                    break;
                }
            }

            if (isok) {

//                String json = NewSZApi.submitWeightInfo(steelyardInfo, szWeightInfos.toArray(new SZWeightInfo[szWeightInfos.size()]), goodEntities.get(0), steelyardInfo.getStallEntity(), null, true);
                return szWeightInfos;

            }
        }
        return null;
    }


}
